package com.fqgj.sentry.manage;

import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.sentry.common.exception.BizException;
import com.fqgj.sentry.common.exception.enums.ErrorCodeEnums;
import com.fqgj.sentry.common.utils.ApiPage;
import com.fqgj.sentry.manage.dao.McApplicationDao;
import com.fqgj.sentry.manage.dao.McGroupManagerDao;
import com.fqgj.sentry.manage.dao.McMerchantDao;
import com.fqgj.sentry.manage.entity.McApplicationEntity;
import com.fqgj.sentry.manage.entity.McGroupManagerEntity;
import com.fqgj.sentry.manage.entity.McMerchantEntity;
import com.fqgj.sentry.manage.service.ApplicationService;
import com.fqgj.sentry.policy.dao.PcPolicySetsDao;
import com.fqgj.sentry.policy.entity.PcPolicySetsEntity;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by ykpbean kangping.ying@yuntu-inc.com
 *
 * @description
 * @create 2017-07-13 下午5:38
 */

@Service
public class ApplicationServiceImpl implements ApplicationService {

    @Autowired
    private McApplicationDao mcApplicationDao;
    @Autowired
    private McMerchantDao mcMerchantDao;
    @Autowired
    private McGroupManagerDao mcGroupManagerDao;
    @Autowired
    private PcPolicySetsDao pcPolicySetsDao;

    @Override
    public McMerchantEntity selectByMerchantId(Long merchantId) {
        return mcMerchantDao.selectByPrimaryKey(merchantId);
    }

    @Override
    public McMerchantEntity selectMerchantByMerchantCode(String code) {
        return mcMerchantDao.selectByCode(code);
    }

    @Override
    public McApplicationEntity selectByMerchantIdAndAppCode(Long merchantId, String appCode) {
        McApplicationEntity applicationEntity = mcApplicationDao.selectByMerchantIdAndAppCode(merchantId, appCode);
        if (applicationEntity == null) {
            throw new BizException(ErrorCodeEnums.app_not_exist_error);
        }
        return applicationEntity;
    }

    @Override
    public McApplicationEntity selectByAppId(Long appId) {
        return mcApplicationDao.selectByPrimaryKey(appId);
    }

    @Override
    public List<McApplicationEntity> selectListForPage(Long merchantId, ApiPage apiPage) {
        Page<McApplicationEntity> page = PageHelper.startPage(apiPage.getPageNum(), apiPage.getPageSize());
        mcApplicationDao.selectListByMerchantId(merchantId);
        apiPage.setTotal(page.getTotal());
        return page.getResult();

    }

    @Override
    public List<McApplicationEntity> selectApplicationList(Long merchantId) {
        return mcApplicationDao.selectListByMerchantId(merchantId);
    }

    @Override
    public Integer deleteById(Long id) {
        List<PcPolicySetsEntity> pcPolicySetsEntities = pcPolicySetsDao.selectByAppIdsAndPage(id);
        if (CollectionUtils.isNotEmpty(pcPolicySetsEntities)) {
            throw new BizException(ErrorCodeEnums.policy_set_is_exit, "该应用还有策略规则无法删除");
        }
        List<McGroupManagerEntity> groupManagerEntities = mcGroupManagerDao.selectByAppId(id);
        if (CollectionUtils.isNotEmpty(groupManagerEntities)) {
            throw new BizException(ErrorCodeEnums.app_group_manager_is_exit_error, "应用还有用户对应无法删除");
        }
        return mcApplicationDao.deleteByPrimaryKey(id);
    }

    @Override
    public Integer insert(McApplicationEntity applicationEntity) {
        //该商户下  该应用编号是否已经存在
        if (mcApplicationDao.selectByMerchantIdAndAppCode(applicationEntity.getMerchantId(),
                applicationEntity.getAppCode()) != null) {
            throw new BizException(ErrorCodeEnums.app_merchant_is_exist_error);
        }

        McApplicationEntity temp = mcApplicationDao.insert(applicationEntity);
        return temp == null ? 0 : 1;
    }

    @Override
    public Integer update(McApplicationEntity applicationEntity) {
        if (mcApplicationDao.selectByPrimaryKey(applicationEntity.getId()) == null) {
            throw new BizException(ErrorCodeEnums.app_not_exist_error);
        }
        return mcApplicationDao.updateByPrimaryKey(applicationEntity);

    }


}
